package com.dai.zhenfujin.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;


public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
		api = WXAPIFactory.createWXAPI(this, Constants.WECHAT_ID, false);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {
//		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code=resp.errCode;
			if(code==0){

				Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();

                finish();

			}else if(code==-1){
				Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
			}else if(code==-2){
				Toast.makeText(this, "取消支付", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}



	public void payReBack() {
		Map<String, String> params = new HashMap<>();
//		params.put("cashTransactionRecordAmount", "1");
//		params.put("cashTransactionRecordSig", "3");
//		params.put("tradeNo", number);
//		params.put("userId", SaveUtil.getInstance().getUserId(this));




//		StringBuilder url = new StringBuilder();
//		url.append("https://api.weixin.qq.com/sns/oauth2/access_token")
//				.append("?appid=")
//				.append(Contacts.WECHAT_ID)
//				.append("&secret=")
//				.append(Contacts.WECHAT_SECRET)
//				.append("&code=")
//				.append(code)
//				.append("&grant_type=authorization_code");
//		Request request = new Request.Builder().url(loginUrl.toString()).build();


//		HttpClient.okHttpClient.newCall(request).enqueue(new Callback() {


	}
}